﻿// SilverlightDesktop.Net - http://www.SilverlightDesktop.net
// Copyright (c) 2008
// by SilverlightDesktop.Net
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using SilverlightDesktopCore.DAL;

public partial class _Default : System.Web.UI.Page
{
    string strCurrentVersion = "01.95.00";
    bool boolSetup = false;

    protected void Page_Load(object sender, EventArgs e)
    {
        boolSetup = DatabaseReady();

        if (boolSetup)
        {
            lnkLogin.Visible = !User.Identity.IsAuthenticated;
            lnkLogOut.Visible = User.Identity.IsAuthenticated;

            ShowSilverlightDesktopLink();

            // Possibly show Administration link
            if (User.Identity.IsAuthenticated && (User.Identity.AuthenticationType == "Forms"))
            {
                FormsIdentity id = (FormsIdentity)User.Identity;
                FormsAuthenticationTicket ticket = id.Ticket;
                lnkAdministration.Visible = (ticket.UserData.ToString() == "Administrators");
            }

            ShowDesktops();
        }
        else
        {
            lnkSetup.Visible = true;
        }
    }

    #region ShowSilverlightDesktopLink
    private void ShowSilverlightDesktopLink()
    {
        GeneralSettings GeneralSettings = new GeneralSettings();
        if (GeneralSettings.AnonymousLogin)
        {
            lnkSilverlightDesktop.Visible = true;
        }
        else
        {
            lnkSilverlightDesktop.Visible = User.Identity.IsAuthenticated;
        }
    }
    #endregion

    #region DatabaseReady
    private bool DatabaseReady()
    {
        // This method returns true if the databse exists and the table is created
        bool CanConnect = true;

        try
        {
            SilverlightDesktopDAL SilverlightDesktopDAL = new SilverlightDesktopDAL();
            var result = from VersionNumber in SilverlightDesktopDAL.Versions
                         select VersionNumber;
            CanConnect = (result.FirstOrDefault().VersionNumber == strCurrentVersion) ? true : false;
        }
        catch (Exception e)
        {
            string strError = e.Message;
            CanConnect = false;
        }

        return CanConnect;
    }
    #endregion

    #region ShowDesktops
    private void ShowDesktops()
    {
        SilverlightDesktopDAL SilverlightDesktopDAL = new SilverlightDesktopDAL();
        var result = from DesktopInstances in SilverlightDesktopDAL.SilverlightDesktopInstances
                     select DesktopInstances;

        GridView1.DataSource = result.ToList();
        GridView1.DataBind();

        GridView1.Visible = (result.Count() > 0) ? true : false;
    }
    #endregion

    #region LogOut
    protected void lnkLogOut_Click(object sender, EventArgs e)
    {
        FormsAuthentication.SignOut();
        Response.Redirect("Default.aspx");
    }
    #endregion
}